package com.zhiwei.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhiwei.mapper.OrderMapper;
import com.zhiwei.pojo.Order;
import com.zhiwei.tool.DateUtil;
import com.zhiwei.tool.StartAndEnd;
import com.zhiwei.vo.OrderInfo;
import com.zhiwei.vo.OrderTab;
import com.zhiwei.vo.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.text.NumberFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Service
public class OrderServiceImpl implements OrderService{
    @Autowired
    private OrderMapper orderMapper;

    @Override
    public Order getOrder(Integer orderId) {
        return orderMapper.selectById(orderId);
    }

    @Override
    public OrderInfo getOrderInfo() {
        SimpleDateFormat dateStart = new SimpleDateFormat("yyyy-01-01 00:00:00");
        SimpleDateFormat dateEnd = new SimpleDateFormat("yyyy-12-31 00:00:00");
        String startDayTime = dateStart.format(new Date());
        String endDayTime = dateEnd.format(new Date());
        QueryWrapper<Order> wrapper = new QueryWrapper<>();
        wrapper.apply("UNIX_TIMESTAMP(created) >= UNIX_TIMESTAMP('"+startDayTime+"')")
        .apply("UNIX_TIMESTAMP(created) < UNIX_TIMESTAMP('"+endDayTime+"')");
        List<Order> orders = orderMapper.selectList(wrapper);
        OrderInfo orderInfo = new OrderInfo();
        Double[] outNum = {0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0};
        Double[] inNum = {0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0};
        Double[] incomeNum = {0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0};
        for (Order order : orders) {
            //获取Calendar对象，用于获取月份
            Calendar calendar = Calendar.getInstance();
            calendar.setTimeInMillis(order.getCreated().getTime());
            int month = calendar.get(Calendar.MONTH) + 1;
            //一月
            if (month == 1){
                if (order.getType().equals("出库")){
                    outNum[0] += order.getNumber() == null ? 0.0 : order.getNumber();
                }else {
                    inNum[0] += order.getNumber() == null ? 0.0 : order.getNumber();
                }
            }
            //二月
            if (month == 2){
                if (order.getType().equals("出库")){
                    outNum[1] += order.getNumber() == null ? 0.0 : order.getNumber();
                }else {
                    inNum[1] += order.getNumber() == null ? 0.0 : order.getNumber();
                }
            }
            //3月
            if (month == 3){
                if (order.getType().equals("出库")){
                    outNum[2] += order.getNumber() == null ? 0.0 : order.getNumber();
                }else {
                    inNum[2] += order.getNumber() == null ? 0.0 : order.getNumber();
                }
            }
            //4月
            if (month == 4){
                if (order.getType().equals("出库")){
                    outNum[3] += order.getNumber() == null ? 0.0 : order.getNumber();
                }else {
                    inNum[3] += order.getNumber() == null ? 0.0 : order.getNumber();
                }
            }
            //5月
            if (month == 5){
                if (order.getType().equals("出库")){
                    outNum[4] += order.getNumber() == null ? 0.0 : order.getNumber();
                }else {
                    inNum[4] += order.getNumber() == null ? 0.0 : order.getNumber();
                }
            }
            //6月
            if (month == 6){
                if (order.getType().equals("出库")){
                    outNum[5] += order.getNumber() == null ? 0.0 : order.getNumber();
                }else {
                    inNum[5] += order.getNumber() == null ? 0.0 : order.getNumber();
                }
            }
            //7月
            if (month == 7){
                if (order.getType().equals("出库")){
                    outNum[6] += order.getNumber() == null ? 0.0 : order.getNumber();
                }else {
                    inNum[6] += order.getNumber() == null ? 0.0 : order.getNumber();
                }
            }
            //8月
            if (month == 8){
                if (order.getType().equals("出库")){
                    outNum[7] += order.getNumber() == null ? 0.0 : order.getNumber();
                }else {
                    inNum[7] += order.getNumber() == null ? 0.0 : order.getNumber();
                }
            }
            //9月
            if (month == 9){
                if (order.getType().equals("出库")){
                    outNum[8] += order.getNumber() == null ? 0.0 : order.getNumber();
                }else {
                    inNum[8] += order.getNumber() == null ? 0.0 : order.getNumber();
                }
            }
            //10月
            if (month == 10){
                if (order.getType().equals("出库")){
                    outNum[9] += order.getNumber() == null ? 0.0 : order.getNumber();
                }else {
                    inNum[9] += order.getNumber() == null ? 0.0 : order.getNumber();
                }
            }
            //11月
            if (month == 11){
                if (order.getType().equals("出库")){
                    outNum[10] += order.getNumber() == null ? 0.0 : order.getNumber();
                }else {
                    inNum[10] += order.getNumber() == null ? 0.0 : order.getNumber();
                }
            }
            //12月
            if (month == 12){
                if (order.getType().equals("出库")){
                    outNum[11] += order.getNumber() == null ? 0.0 : order.getNumber();
                }else {
                    inNum[11] += order.getNumber() == null ? 0.0 : order.getNumber();
                }
            }
        }
        for (int i = 0; i < outNum.length; i++) {
                incomeNum[i]=outNum[i]*100;
        }
        orderInfo.setInNum(inNum).setOutNum(outNum).setIncomeNum(incomeNum);
        return orderInfo;
    }

    @Override
    public PageResult getBillList(PageResult pageResult) {
        IPage page = new Page(pageResult.getPageNum(),pageResult.getPageSize());
        StartAndEnd dates = DateUtil.getDates(pageResult.getStart(), pageResult.getEnd());
        QueryWrapper<Order> wrapper = new QueryWrapper<>();
        wrapper.eq("status", pageResult.getStatus())
                .eq("type", pageResult.getType()==1?"出库":"入库")
                .eq("delete_flag", 0)
                .eq(StringUtils.hasLength(pageResult.getQuery()),"id", pageResult.getQuery())
                .orderByDesc("created")
                .apply(StringUtils.hasLength(dates.getStart()),"UNIX_TIMESTAMP(created) >= UNIX_TIMESTAMP('"+dates.getStart()+"')")
                .apply(StringUtils.hasLength(dates.getEnd()),"UNIX_TIMESTAMP(created) < UNIX_TIMESTAMP('"+dates.getEnd()+"')");
        IPage iPage = orderMapper.selectPage(page, wrapper);
        List<Order> orders = iPage.getRecords();
        return pageResult.setRows(orders).setTotal(iPage.getTotal());
    }

    @Override
    public OrderTab[] getThisMonth() {
        Calendar instance = Calendar.getInstance();
        instance.setTime(new Date());
        int thisMonth = instance.get(Calendar.MONTH) + 1;
        instance.add(Calendar.MONTH, -2);
        Date start = instance.getTime();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-31 00:00:00");
        String endDayTime = dateFormat.format(new Date());
        String startDayTime = dateFormat.format(start);
        QueryWrapper<Order> wrapper = new QueryWrapper<>();
        wrapper.apply("UNIX_TIMESTAMP(created) >= UNIX_TIMESTAMP('"+startDayTime+"')")
                .apply("UNIX_TIMESTAMP(created) < UNIX_TIMESTAMP('"+endDayTime+"')");
        List<Order> orders = orderMapper.selectList(wrapper);
        OrderTab OrderTab[] = {
                new OrderTab().setTitle("本月入库").setNum(0),
                new OrderTab().setTitle("本月订单").setNum(0),
                new OrderTab().setTitle("本月销售额").setNum(0)};
        Double lastNum0 = 0.0;
        Double lastNum1 = 0.0;
        Double lastNum2 = 0.0;
        for (Order order : orders) {
            instance.setTimeInMillis(order.getCreated().getTime());
            int month = instance.get(Calendar.MONTH) + 1;
            if (month == thisMonth && order.getType().equals("入库")){
                OrderTab[0].setNum(OrderTab[0].getNum()+order.getNumber());
            }
            if (month == thisMonth && order.getType().equals("出库")){
                OrderTab[1].setNum(OrderTab[1].getNum()+order.getNumber());
                OrderTab[2].setNum(OrderTab[2].getNum()+order.getNumber()*100);
            }
            if (month == thisMonth-1 && order.getType().equals("入库")){
                lastNum0 += order.getNumber();
            }
            if (month == thisMonth-1 && order.getType().equals("出库")){
                lastNum1 += order.getNumber();
                lastNum2 += order.getNumber()*100;
            }
        }
        NumberFormat nt = NumberFormat.getPercentInstance();//获取格式化对象
        nt.setMinimumFractionDigits(2);//设置百分数精确度2即保留两位小数
        OrderTab[0].setPersent(nt.format( Math.abs(OrderTab[0].getNum() - lastNum0)/ lastNum0));
        OrderTab[1].setPersent(nt.format( Math.abs(OrderTab[1].getNum() - lastNum1)/ lastNum1));
        OrderTab[2].setPersent(nt.format( Math.abs(OrderTab[2].getNum() - lastNum2)/ lastNum2));
        if (OrderTab[0].getNum()>=lastNum0){
           OrderTab[0].setState("1");
       }else {
           OrderTab[0].setState("0");
       }
        if (OrderTab[1].getNum()>=lastNum1){
            OrderTab[1].setState("1");
        }else {
            OrderTab[1].setState("0");
        }
        if (OrderTab[2].getNum()>=lastNum2){
            OrderTab[2].setState("1");
        }else {
            OrderTab[2].setState("0");
        }
        return OrderTab;
    }
}
